import { defineStore } from 'pinia'

export const useMapDataStore = defineStore('mapData', {
  state: () => ({
    mapData: [],
    currentLocation: '北京市',
    currentAqi: [64, 51, 56, 0.862, 20, 15, 8]
  }),
  getters: {
    // 使用getters来定义计算属性
    topTenByCount: (state) => {
      return state.mapData
        .map((item) => ({ name: item.gname, value: item.count }))
        .sort((a, b) => b.value - a.value) // 降序排序
        .slice(0, 10) // 取前10个
    },
    topTenPercent: (state) => {
      const sortedData = state.mapData
        .map((item) => ({ name: item.gname, value: item.count }))
        .sort((a, b) => b.value - a.value)

      return sortedData.slice(0, 10).map((item) => ({
        ...item,
        value: ((item.value / 10) * 100).toFixed(2) // 计算百分比并保留两位小数
      }))
    }
  }
})
